package com.hs.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Psych;
import com.hs.sfzy.service.PsychService;
import com.hs.sfzy.until.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("psych")
public class PsychController {
    @Autowired
    PsychService psychService;

    @PostMapping
    public R insert(@RequestBody Psych psych) {
        int insert = psychService.insert(psych);
        if (insert > 0) {
            return R.ok();
        }
        return R.fail();
    }
    @PutMapping
    public R update(@RequestBody Psych psych) {
        int update = psychService.update(psych);
        if (update > 0) {
            return R.ok();
        }
        return R.fail();
    }
    @DeleteMapping("{id}")
    public R delete(@PathVariable("id") int id) {
        int delete = psychService.delete(id);
        if (delete > 0) {
            return R.ok();
        }
        return R.fail();
    }
    @GetMapping("{id}")
    public R get(@PathVariable("id") int id) {
        Psych psych = psychService.selectById(id);
        return R.ok(psych);
        //
    }
    @GetMapping("page")
    public R getPage(@RequestParam(required = false,defaultValue = "1") int pageNum,
                     @RequestParam(required = false, defaultValue = "3") int pageSize) {
        PageInfo<Psych> psychPageInfo = psychService.selectAll(pageNum, pageSize);
        return R.ok(psychPageInfo);
    }
    // 简单场景：前端传psychId
    @PutMapping("/updateReplyCount")
    public R updateReplyCount(@RequestParam int psychId) {
        int rows = psychService.incrementReplyCount(psychId);
        if (rows > 0) {
            return R.ok();
        }
        return R.fail();
    }
}
